package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pojo.Author;
import pojo.Book;
import pojo.Topic;
import pojo.Translator;
import pojo.User;

import java.util.*;

import dao.AuthorDAO;
import dao.BookDAO;
import dao.CommentDAO;
import dao.TopicDAO;
import dao.TranslatorDAO;
import dao.UserDAO;
 
@Controller
public class DichGiaController {
	private int id=-1;
	private static int btt=0;
	private static int page=0;
	private static int kieuSort=0;
    @RequestMapping(value= "/DichGia.do")
    public String getview(HttpServletRequest request,
    		@RequestParam(value="dichgiaid", required = false) String dichgiaid,
    		@RequestParam(value ="trang", required = false) String trang,
			@RequestParam(value ="slbooktt", required = false) String _btt,
			@RequestParam(value ="kieuSort", required = false) String _kieusort,
			@RequestParam(value ="save", required = false) String save,
			Model model
    		) {
    	if(save.equals("false"))
    	{
    		 id=-1;
    		 btt=0;
    		 page=0;
    		kieuSort=0;
    	}
    	if(dichgiaid!=null) id=Integer.parseInt(dichgiaid);
    	if(_btt!=null)    	btt=Integer.parseInt(_btt);
    	if(trang!=null) page=Integer.parseInt(trang);
    	if(_kieusort!=null) kieuSort=Integer.parseInt(_kieusort);
    	TranslatorDAO td= new TranslatorDAO();
    	Translator a= td.getTranslatorByID(id);
    	
    	if(a==null) return "home";
    	BookDAO bd=new BookDAO();
    	long tongsotrang= bd.tinhSoTrangTheoDichGia(a, btt);
    	List<Book> lb= bd.getBookTheoDichGia(a, page, btt, kieuSort);
    	model.addAttribute("lb",lb);
    	model.addAttribute("Servlet","DichGia.do?save=true");
    	model.addAttribute("tongSoTrang",tongsotrang);
    	model.addAttribute("trangHienTai",page);
    	model.addAttribute("slbooktt",btt);
    	model.addAttribute("kieuSort",kieuSort);
    	model.addAttribute("title1","Sách theo tác giả \""+a.getName()+"\"");
    	model.addAttribute("title2",null);
    	model.addAttribute("tukhoa",null);
    	
    	
    	return ("page/FrontEnd/KetQuaTimKiem");
    }
   
 
   
}